<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>算法解析：存在重复元素</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .string { color: #98c379; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; font-style: italic; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .fade-in {
            animation: fadeIn 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-code mr-4"></i>存在重复元素
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                探索高效的数组去重算法，掌握哈希表与排序的巧妙应用
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <div class="flex items-center mb-6">
                <div class="bg-purple-100 p-3 rounded-full mr-4">
                    <i class="fas fa-question-circle text-purple-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">给</span>定一个整数数组，判断是否存在重复元素。如果数组中任何元素出现至少两次，函数返回 true；如果数组中每个元素都不相同，则返回 false。
            </p>
            <div class="mt-6 bg-gray-50 rounded-lg p-6">
                <h3 class="text-xl font-semibold text-gray-800 mb-3">
                    <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>示例
                </h3>
                <div class="flex items-center space-x-4">
                    <span class="bg-blue-100 text-blue-800 px-4 py-2 rounded-lg font-mono">
                        输入: [1, 2, 3, 1]
                    </span>
                    <i class="fas fa-arrow-right text-gray-400"></i>
                    <span class="bg-green-100 text-green-800 px-4 py-2 rounded-lg font-mono">
                        输出: true
                    </span>
                </div>
                <p class="text-gray-600 mt-3">因为值 1 出现了两次</p>
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-3 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover fade-in">
                <div class="bg-indigo-100 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                    <i class="fas fa-hashtag text-indigo-600 text-xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-2">哈希表</h3>
                <p class="text-gray-600">利用集合的唯一性特征，快速检测重复元素</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover fade-in" style="animation-delay: 0.1s">
                <div class="bg-green-100 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                    <i class="fas fa-layer-group text-green-600 text-xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-2">集合</h3>
                <p class="text-gray-600">Python内置数据结构，自动去重的完美工具</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover fade-in" style="animation-delay: 0.2s">
                <div class="bg-yellow-100 p-3 rounded-full w-12 h-12 flex items-center justify-center mb-4">
                    <i class="fas fa-sort text-yellow-600 text-xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-2">排序</h3>
                <p class="text-gray-600">通过排序使相同元素相邻，便于比较检测</p>
            </div>
        </div>

        <!-- Solution Approaches -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 flex items-center">
                <i class="fas fa-brain text-purple-600 mr-3"></i>解题思路
            </h2>
            
            <!-- Approach 1: Hash Table -->
            <div class="mb-8">
                <div class="flex items-start mb-4">
                    <div class="bg-blue-500 text-white rounded-full w-8 h-8 flex items-center justify-center font-bold mr-3 flex-shrink-0">
                        1
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold text-gray-800 mb-3">哈希表方法</h3>
                        <p class="text-gray-700 mb-4">使用集合记录已遍历的元素，若当前元素已存在于集合中，则返回 True。</p>
                        <div class="bg-gray-50 rounded-lg p-4 flex items-center justify-between">
                            <div class="flex items-center">
                                <i class="fas fa-clock text-blue-500 mr-2"></i>
                                <span class="text-gray-700">时间复杂度：<strong>O(n)</strong></span>
                            </div>
                            <div class="flex items-center">
                                <i class="fas fa-memory text-green-500 mr-2"></i>
                                <span class="text-gray-700">空间复杂度：<strong>O(n)</strong></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Approach 2: Sorting -->
            <div>
                <div class="flex items-start">
                    <div class="bg-green-500 text-white rounded-full w-8 h-8 flex items-center justify-center font-bold mr-3 flex-shrink-0">
                        2
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold text-gray-800 mb-3">排序方法</h3>
                        <p class="text-gray-700 mb-4">先排序数组，然后检查相邻元素是否相同。</p>
                        <div class="bg-gray-50 rounded-lg p-4 flex items-center justify-between">
                            <div class="flex items-center">
                                <i class="fas fa-clock text-blue-500 mr-2"></i>
                                <span class="text-gray-700">时间复杂度：<strong>O(n log n)</strong></span>
                            </div>
                            <div class="flex items-center">
                                <i class="fas fa-memory text-green-500 mr-2"></i>
                                <span class="text-gray-700">空间复杂度：<strong>O(1)</strong></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Algorithm Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-project-diagram text-purple-600 mr-3"></i>算法流程图
            </h2>
            <div class="mermaid">
                graph TD
                    A[开始] --> B[输入数组 nums]
                    B --> C{选择算法}
                    C -->|哈希表方法| D[创建空集合 seen]
                    C -->|排序方法| E[对数组排序]
                    D --> F[遍历数组元素]
                    F --> G{元素是否在集合中?}
                    G -->|是| H[返回 True]
                    G -->|否| I[将元素加入集合]
                    I --> J{还有元素?}
                    J -->|是| F
                    J -->|否| K[返回 False]
                    E --> L[遍历相邻元素]
                    L --> M{相邻元素相同?}
                    M -->|是| H
                    M -->|否| N{还有相邻对?}
                    N -->|是| L
                    N -->|否| K
                    H --> O[结束]
                    K --> O
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-code text-purple-600 mr-3"></i>示例代码
            </h2>
            <p class="text-gray-600 mb-4">使用哈希表方法的优雅实现：</p>
            <div class="code-block">
                <pre><code><span class="keyword">def</span> <span class="function">containsDuplicate</span>(nums):
    <span class="keyword">return</span> <span class="function">len</span>(nums) != <span class="function">len</span>(<span class="function">set</span>(nums))</code></pre>
            </div>
            <div class="mt-6 bg-blue-50 rounded-lg p-6">
                <h3 class="text-xl font-semibold text-gray-800 mb-3">
                    <i class="fas fa-info-circle text-blue-600 mr-2"></i>代码解析
                </h3>
                <ul class="space-y-2 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                        <span><code class="bg-gray-200 px-2 py-1 rounded">set(nums)</code> 将数组转换为集合，自动去除重复元素</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                        <span>比较原数组长度与集合长度，若不相等则存在重复</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                        <span>一行代码优雅解决问题，体现Python的简洁之美</span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Extended Solutions -->
        <div class="bg-white rounded-2xl shadow-xl p-8 card-hover fade-in">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-puzzle-piece text-purple-600 mr-3"></i>扩展解法
            </h2>
            
            <!-- Traditional Hash Table -->
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4">传统哈